package com.tencent.wepay.handler.validation;

import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tencent.wepay.annotation.NotNull;
import com.tencent.wepay.handler.ValidationHandler;
import com.tencent.wepay.protocol.domain.Validation;

public class NotNullHandler implements ValidationHandler{
	private final static Logger logger=LoggerFactory.getLogger(NotNullHandler.class);
	@Override
	public boolean handler(Validation v,Field field) {
		String fieldName=field.getName();
		NotNull notNull=(NotNull) field.getAnnotation(NotNull.class);
		try {
			Object value=field.get(v);
			if(value==null){
				logger.error("["+fieldName+"]"+notNull.desc());
				return false;
			}
		}catch (IllegalArgumentException | IllegalAccessException e) {
			return false;
		}
		return true;
	}

	@Override
	public boolean access(Field field) {
		return true;
	}
	
}
